package com.software.loginregister.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.NoteDraft;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class NoteDraftsAdapter extends RecyclerView.Adapter<NoteDraftsAdapter.ViewHolder> {
    private Context context;
    private List<NoteDraft> noteDrafts;
    private SimpleDateFormat dateFormat;

    public NoteDraftsAdapter(Context context, List<NoteDraft> noteDrafts) {
        this.context = context;
        this.noteDrafts = noteDrafts;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_note_draft, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NoteDraft draft = noteDrafts.get(position);
        holder.tvTitle.setText(draft.getTitle());
        holder.tvTime.setText(dateFormat.format(new Date(draft.getCreateTime())));
        // TODO: 使用图片加载库加载图片
        // Glide.with(context).load(draft.getImageUrl()).into(holder.ivImage);
    }

    @Override
    public int getItemCount() {
        return noteDrafts.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        TextView tvTitle;
        TextView tvTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_note_image);
            tvTitle = itemView.findViewById(R.id.tv_note_title);
            tvTime = itemView.findViewById(R.id.tv_note_time);
        }
    }
}